﻿using System;
using System.Collections.Generic;
using System.Text;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.Telnet.Commands
{
    /// <summary>
    /// 指定终端交互系统命令
    /// </summary>
    [TelnetCommandAttribute(TelnetCommandConstant.TERMINAL_OS_COMMAND)]
    internal class TerminalOSCommand : TelnetCommandAbs
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public TerminalOSCommand()
            : base()
        {

        }


        /// <summary>
        /// 执行命令[执行成功返回true;否则返回false]
        /// </summary>
        /// <param name="telnetClient">执行命令的连接</param>
        /// <param name="paraArr">命令参数</param>
        /// <returns>执行结果</returns>
        protected override bool PrimitiveExcute(IListenTelnetClient telnetClient, string[] paraArr)
        {
            if (paraArr.Length < 3)
            {
                return false;
            }

            int value;
            if (!int.TryParse(paraArr[2], out value))
            {
                return false;
            }

            try
            {
                var platformID = (PlatformID)value;
                OSPlatformID osPlatformID = EnvironmentEx.CovnerTo(platformID);
                switch (osPlatformID)
                {
                    case OSPlatformID.Win:
                    case OSPlatformID.Linux_Unix:
                    case OSPlatformID.MacOS:
                        break;
                    default:
                        throw new NotImplementedException();
                }

                telnetClient.Platform = osPlatformID;
                telnetClient.SendMessage(TelnetConstant.END_LINE_CURSOR);
                return true;
            }
            catch (Exception ex)
            {
                ZLoger.Warn(ex);
                telnetClient.SendMessage($"{TelnetConstant.END_LINE_CURSOR}{ex.Message}{TelnetConstant.END_LINE_CURSOR}");
                return false;
            }
        }
    }
}
